package com.yunhe.mybatis.service.impl;

import com.yunhe.mybatis.common.utils.SqlSessionManager;
import com.yunhe.mybatis.dao.mett.MeetDao;
import com.yunhe.mybatis.dto.Pageable;
import com.yunhe.mybatis.entity.Meet;

import com.yunhe.mybatis.service.MeetService;

import java.util.List;

public class MeetServiceImpl implements MeetService {
    @Override
    public void save(Meet meet) {
        MeetDao meetDao = SqlSessionManager.getSqlSession().getMapper(MeetDao.class);
        meetDao.save(meet);
    }

    @Override
    public void update(Meet meet) {
        MeetDao meetDao = SqlSessionManager.getSqlSession().getMapper(MeetDao.class);
        meetDao.update(meet);
    }

    @Override
    public void remove(Integer id) {
        MeetDao meetDao = SqlSessionManager.getSqlSession().getMapper(MeetDao.class);
        meetDao.remove(id);
    }

    @Override
    public List<Meet> findAll() {
        MeetDao meetDao = SqlSessionManager.getSqlSession().getMapper(MeetDao.class);
        return  meetDao.findAll();
    }

    @Override
    public Meet find(Integer id) {
        MeetDao meetDao = SqlSessionManager.getSqlSession().getMapper(MeetDao.class);
        return meetDao.find(id);
    }

    @Override
    public Pageable findForPage(int page, int size, String name, Integer typee) {
        MeetDao meetDao = SqlSessionManager.getSqlSession().getMapper(MeetDao.class);
        Pageable pageable = new Pageable();
        pageable.setCurrent(page);
        pageable.setSize(size);
        int count = meetDao.count();
        pageable.setCount(count);
        if(count > 0 ){
            name = name != null ? "%" + name +"%" : name;
            List<Meet> list = meetDao.findList((page - 1) * size, size,  name,typee);
            pageable.setRows(list);

            int pageCount = count/size;
            pageCount = count % size == 0 ? pageCount : pageCount + 1;
            pageable.setPageCount(pageCount);
        }
        return pageable;
    }


}
